
#include "town.h" // class's header file
#include "sdlUtilities.h"


//#define GP2x


  
  
#ifdef GP2x
     #define RETURNBUTTON VK_FB
     #define UPBUTTON     VK_UP
     #define DOWNBUTTON   VK_DOWN
     #define RIGHTBUTTON  VK_RIGHT
     #define LEFTBUTTON   VK_LEFT
     #define END          VK_START
     #define SLEEP 5
#else
     #define RETURNBUTTON SDLK_a
     #define UPBUTTON     SDLK_UP
     #define DOWNBUTTON   SDLK_DOWN
     #define RIGHTBUTTON  SDLK_RIGHT
     #define LEFTBUTTON   SDLK_LEFT
     #define END          SDLK_ESCAPE
     #define SLEEP 20
#endif


town::town()
{
            picbx=0;
            picby=0;
            picentprell=0;
}

town::~town()
{
}

void town::run()
{
     draw();
     key=getKeyEvent();
     switch(key)
     {
                case UPBUTTON:
                     checkcoll(0,-2);
                     break;
                case DOWNBUTTON:
                     checkcoll(0,2);
                     break;
                case RIGHTBUTTON:
                     checkcoll(2,0);
                     break;
                case LEFTBUTTON:
                     checkcoll(-2,0);
                     break;
                case RETURNBUTTON:
                     if(entprell)
                     {
                                 //getKeyEventEnt();
                                 checkEv();
                                 
                     }
                     break;
                case END:
                     *done=1;
                     break;
     }
     SDL_Delay(SLEEP);
}


void town::load (char loaddesc [])
{
     FILE *fp;
     int screenx, screeny,picx,picy;
     char temp [40];
     fp=fopen (loaddesc, "r");
     fscanf(fp,"%d %d %d %d %s",&screenx,&screeny,&picx,&picy,temp);
     init (screenx, screeny,picx,picy, temp);
     fscanf(fp,"%s", temp);
     fclose(fp);
     list=fopen(temp, "r");
     fscanf (list,"%s", temp);
     understandlist(temp);
     
     
}


void town::init (int screenx, int screeny, int picx, int picy, char pic [])
{
     width= screenx;
     height = screeny;
     picsizex=picx;
     picsizey=picy;
     check = new bool [picsizex*picsizey];
     event = new int [(picsizex/8)*(picsizey/8)];
     
     drawx=60;
     drawy=picy-screenx;
     imageLoadSdl(50, pic);
     imageLoadSdl(60, "./Grafik/town/Soji.b");
     imageLoadSdl(20, "./Grafik/town/text.b");
     
     
}

void town::draw ()
{
     imageDrawSdl( 50, drawx, drawy, width, height, 0,0);
     imageDrawSdl( 60, picbx, picby, 35, 40, 130, 190, 255,255);
     
     chars [0].draw(drawx, drawy, width, height);
     chars [1].draw(drawx, drawy, width, height);
     
     flipSdl();
}

void town::drawcam (int drawx, int drawy, int drawpeople)
{
     this->camx=drawx;
     this->camy=drawy;
     this->drawpeople=drawpeople;
     
     imageDrawSdl( 50, drawx, drawy, width, height, 0,0);
     peoplepos=0;
    do
    { 
     chars [peoplepos].draw(drawx, drawy, width, height);
     peoplepos++;
    }while (peoplepos <= drawpeople);
     
     flipSdl();
}

void town::drawcam ()
{
     imageDrawSdl( 50, camx, camy, width, height, 0,0);
     peoplepos=0;
    do
    { 
     chars [peoplepos].draw(camx, camy, width, height);
     peoplepos++;
    }while (peoplepos <= drawpeople);
     
     flipSdl();
}


void town::checkcoll(int x, int y)
{
     checkx=drawx+x;
     checky=drawy+y;
     int xc=15;
     
  for (int yc=10; yc<30;yc++)   
     //for (int xc=15;xc< 30;xc++)
     {
         
                             if( check [((picsizex)*((checky+190)+(yc-1)))+(checkx+xc+130)] == 1)
                              { 
                                      return;}
                             
     }
     
     
     if (checkx == 0 || checkx == (picsizex-width))
     {
                return;
     }
     else
     {
         //for (int xc=;x
          if(x>0)
           picby=40;
          if(x<0)
            picby=0;
            
           drawx=drawx+x;
           
         picentprell++;
         if(picentprell == 3)
         {  
           if(picbx==0)
               picbx=35;
           else
               picbx=0;
           picentprell=0;
         }
     }
     
    if (checky == 0 || checky == (picsizey- height))
     {
                return;
     }
     else
     {
          
           drawy=drawy+y;
           
     }
     
}

void town::understandlist( char command [])
{
     if (!strcmp (command, "PEOPLE"))
     {
                 printf ("\n%s ",command);
                 
                 int number;
                 fscanf (list, "%d",&number);
                 fscanf (list, "%d/%d %d/%d %s", &chars[number].x,&chars[number].y,&chars[number].width, &chars[number].height,chars[number].picture);
                 
                 printf("%d/%d %d/%d %s\n", chars[number].x,chars[number].y,chars[number].width, chars[number].height,chars[number].picture);
                 
                 imageLoadSdl(number+900,chars[number].picture);
                 chars[number].slot=900+number;
                 for (int y=0; y<chars[number].height;y++)
                    for (int x=0; x<chars[number].width;x++)
                     {
                             check [((picsizex)*(chars[number].y+(y-1)))+chars[number].x+x] = 1;
                             
                     }
                 fscanf (list, "%s", command);
                 if (!strcmp(command,"TEXT"))
                 {
                               printf ("\n\t%s",command);
                               
                                  for (int sy=-4;sy<5;sy++)   
                                     for(int sx=-3;sx<4;sx++)
                                     {
                                             event[((picsizex/8)*((chars[number].y/8)-1+sy))+(chars[number].x/8)+sx]=(10+number);
                                             //printf("%d ",check[((picsizex/8)*((chars[number].y/8)-1+sy))+(chars[number].x/8)+sx]);
                                     }
                                            
             
                                    fscanf(list, "%s",  chars[number].text);
                                    
                                    chars[number].action = &town::textout;
                                    chars[number].textfilter();
                                    printf( " %s",  chars[number].text);
                 }
                 printf("\n");
                 fscanf (list,"%s",command);
                 understandlist(command);  
     }
     else if (!strcmp (command, "END"))
     {
     }
    
     
}

void town::textout( people obj)
{
     char text [27]={0};
     for (int k=0;k<strlen(obj.text);)
     {
          for (int i=0; i<=25;i++)
          {
              text[i]=obj.text[k];
              k++;
         
          }
          printf("%d/%d",k,strlen(obj.text)); 
          imageDrawSdl(20, 0,0,200,20,18,8);
          textSdl(20,10, 200, 20, "verdana.ttf", 12, WHITE, text);
          printf("text: %s", text);
          flipSdl();
          //SDL_Delay(250);
          waitKeyUp();
          while (getKeyEventEnt() != RETURNBUTTON);
          
          
     }
     waitKeyUp();
}

void town::checkEv()
{
     int px=(drawx+130)/8;
     int py=(drawy+190)/8;
     printf ("\n");
/*   for( int l=0;l<(picsizey/8);l++) 
   { 
     for (int k=0;k<(picsizey/8);k++)
      printf ("%d ",event[((picsizex/8)*(l))+px+k]);
     printf ("\n");
   }
   printf("___________________________________________________");*/
     if (picby==40)
        px+=4;
     else
         px--;
         
     printf ("Check: %d/%d//%d",py,px,event [((picsizex/8)*(py-1))+px]);
     if(event [((picsizex/8)*(py-1))+px] !=0)
          (chars[(event [((picsizex/8)*(py-1))+px])-10].action)(chars [(event [((picsizex/8)*(py-1))+px])-10]);
     
}


/*###################################################################################################*/

void obj::draw(int drawx,int drawy,int width,int height)
{
     
}
    
void obj::move(int mx,int my)
{
      
}

obj::obj()
{
          
}

/*########################################################################*/



void people::draw(int drawx,int drawy,int width,int height)
{
     dx=x-drawx;
     dy=y-drawy;
     
     if (dx>=0 && dx <width)
     {
              if (dy>=0 && dy <height)
              {
                       imageDrawSdl(slot, 0, 0, 35, 40, dx,dy, 255, WHITE);
              }
     }
     else if (dx<0)
     {
              if (dy>=0 && dy <height)
              {
                       imageDrawSdl(slot, -dx, 0, 35+dx, dy, 0,dy, 255, WHITE);
              }
     }
}
void people::move(int mx, int my)
{
     int movex,movey;
     
     if (mx>0)
              movex=-1;
     else
              movex=1;
              
     if (my>0)
              movey=-1;
     else
              movey=1;                 
     
     while (mx!=0 || my!=0)
     {
           x-= 2*movex;
           y-= 2*movey; 
     }
}
people::people()
{
                show=0;
}

void people::textfilter()
{
     char temp [255];
     //sprintf(temp, "%s", text);
  for (int k=0;k<strlen(text);)
     {              
        for (int i=0;i<25;i++)
        {
            
            if(text[k]=='_')
            {
                           text[k]=' ';
                           /*if(i>20)
                                   while (i!=25 && k<strlen(temp)) 
                                   {
                                         k++;
                                         i++;
                                         temp[k]=' ';
                                   }*/
            }
            k++;
        }
     }
     //sprintf(text, "%s", temp);            
}



/*########################################################################################################*/

headquarter::headquarter()
{
                          f=fopen("./data/script/index.wtf", "r");
                          fscanf(f,"%s", input);
                          understand ();
}

void headquarter::understand()
{
    if(input[0] == 'T')
    { 
                if (!strcmp(input, "TOWNLOAD"))
                {
                        fscanf(f,"%s", input);
                        thetown.load(input);
                        printf("\nTownload %s",input);
                }
                else if (!strcmp(input, "TOWNDRAW"))
                {
                     thetown.draw();
                     printf("\nTowndraw");
                }
                else if (!strcmp(input, "TMYPMOVE"))
                {
                     int mx,my;
                     fscanf(f, "%d,%d",&mx,&my);
                     printf("\nTmyPmove %d,%d",mx,my);
                     
                     while (mx!=0 || my!=0)
                     {
                           if(mx > 0)
                           {
                                 mx-=2;
                                 thetown.checkcoll(2,0);
                                 thetown.draw();
                           }
                           else if (mx == 0);
                           else
                           {
                                 mx+=2;
                                 thetown.checkcoll(-2,0);
                                 thetown.draw();
                           }
                           if(my > 0)
                           {
                                 my-=2;
                                 thetown.checkcoll(0,2);
                                 thetown.draw();
                           }
                           else if (my == 0);
                           else
                           {
                                 my+=2;
                                 thetown.checkcoll(0,-2);
                                 thetown.draw();
                           }
                       SDL_Delay(SLEEP);
                     }
                     
                }
                else if(!strcmp(input, "TPMOVE"))
                {
                     int people, mx,my;
                     fscanf(f, "%d,%d %d",&mx,&my, &people);
                     
                     int number=people;
                     
                     for (int y=0; y<thetown.chars[number].height;y++)
                         for (int x=0; x<thetown.chars[number].width;x++)
                         {
                                  thetown.check [((thetown.picsizex)*(thetown.chars[number].y+(y-1)))+thetown.chars[number].x+x] = 0;
                             
                         }
                               
                     for (int sy=-4;sy<5;sy++)   
                         for(int sx=-3;sx<4;sx++)
                         {
                                  thetown.event[((thetown.picsizex/8)*((thetown.chars[number].y/8)-1+sy))+(thetown.chars[number].x/8)+sx]=0;
                         }
                     
                     while (mx!=0 || my!=0)
                     {
                           if(mx > 0)
                           {
                                 mx-=2;
                                 thetown.chars[people].x+=2;
                           }
                           else if (mx == 0);
                           else
                           {
                                 mx+=2;
                                 thetown.chars[people].x-=2;
                       
                           }
                           if(my > 0)
                           {
                                 my-=2;
                                 thetown.chars[people].y+=2;
                          
                           }
                           else if (my == 0);
                           else
                           {
                                 my+=2;
                                 thetown.chars[people].y-=2;
                                 
                           }
                       thetown.drawcam();
                       SDL_Delay(SLEEP);
                     }
                     
                     for (int y=0; y<thetown.chars[number].height;y++)
                         for (int x=0; x<thetown.chars[number].width;x++)
                         {
                                  thetown.check [((thetown.picsizex)*(thetown.chars[number].y+(y-1)))+thetown.chars[number].x+x] = 1;
                             
                         }
                               
                     for (int sy=-4;sy<5;sy++)   
                         for(int sx=-3;sx<4;sx++)
                         {
                                  thetown.event[((thetown.picsizex/8)*((thetown.chars[number].y/8)-1+sy))+(thetown.chars[number].x/8)+sx]=(10+number);
                         }
                     
                     
                     
                }
                else if (!strcmp(input, "TCAM"))
                {
                     int drx,dry,people;
                     fscanf(f, "%d,%d %d",&drx,&dry, &people);
                     thetown.drawcam(drx,dry,people);
                     
                }
                          
    }
    else if(!strcmp(input, "WAIT"))
    {
                  int time;
                  fscanf(f,"%d", &time);
                  printf("\nSleep %d",time);
                  SDL_Delay(time);
    }
                      
    
     if (strcmp (input, "END")!=0)
     {
                printf("; Loadnext");
                loadnext();
     }
     
}

void headquarter::loadnext()
{
     fscanf(f,"%s", input);
     understand ();
}

void headquarter::run(void)
{
     thetown.run();
}
